package cn.hbads.support.structure.number;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * <b> 数字工具类，用来处理一些简单的数字需求 </b>
 *
 * @author : yvvb
 * @since : 2025/5/22
 */
public final class NumberUtil {
    private static final ThreadLocal<DecimalFormat> THREAD_LOCAL_FORMAT =
            ThreadLocal.withInitial(() -> new DecimalFormat("#0.00"));
    private static final String[] CHINESE_DIGITS = {
            "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"
    };
    private static final String[] CHINESE_UNITS = {
            "", "十"
    };
    private NumberUtil() {
    }

    public static String toChineseNumber(int num) {
        if (num < 0 || num > 100) {
            throw new IllegalArgumentException("数字超出范围（0-100）");
        }

        if (num == 0) return CHINESE_DIGITS[0];
        if (num == 10) return "十";
        if (num == 100) return "一百";

        StringBuilder sb = new StringBuilder();

        // 处理十位
        int tens = num / 10;
        if (tens > 0) {
            // 11-19的特殊处理（不显示"一十"）
            sb.append(tens == 1 && num % 10 != 0 ? "" : CHINESE_DIGITS[tens]);
            sb.append(CHINESE_UNITS[1]);
        }

        // 处理个位
        int ones = num % 10;
        if (ones > 0) {
            sb.append(CHINESE_DIGITS[ones]);
        }

        return sb.toString();
    }

    public static String formatBigDecimalToHex2(BigDecimal amount) {
        if (amount == null) {
            return "0.00";
        }
        return THREAD_LOCAL_FORMAT.get().format(amount);
    }
}
